﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Projekt;
using Projekt.Actors;
using System.Threading;

namespace Edytor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Engine game;
        System.Windows.Threading.DispatcherTimer gameStartedChecker;
        System.Windows.Threading.DispatcherTimer gameStoppedChecker;
        System.Windows.Threading.DispatcherTimer tbDaneKomponentuUpdater;

        public MainWindow()
        {
            setTimers();

            gameStartedChecker.Start();

            new Thread(new ThreadStart(() => { game = new Engine(); game.Run(); })).Start();
            
            InitializeComponent();

            gbDaneKomponentu.Visibility = System.Windows.Visibility.Collapsed;
            gbOpcjeKomponentu.Visibility = System.Windows.Visibility.Collapsed;

            this.Closed += new EventHandler(MainWindow_Closed);
        }

        #region Żywot gry i projektu
        void setTimers()
        {
            gameStartedChecker = new System.Windows.Threading.DispatcherTimer();
            gameStartedChecker.Interval = new TimeSpan(0, 0, 0, 0, 10);
            gameStartedChecker.Tick += new EventHandler(gameStartedChecker_Tick);

            gameStoppedChecker = new System.Windows.Threading.DispatcherTimer();
            gameStoppedChecker.Interval = new TimeSpan(0, 0, 0, 0, 100);
            gameStoppedChecker.Tick += new EventHandler(gameStoppedChecker_Tick);

            tbDaneKomponentuUpdater = new System.Windows.Threading.DispatcherTimer();
            tbDaneKomponentuUpdater.Interval = new TimeSpan(0, 0, 0, 0, 100);
            tbDaneKomponentuUpdater.Tick += new EventHandler(tbDaneKomponentuUpdater_Tick);
        }

        void gameStartedChecker_Tick(object sender, EventArgs e)
        {
            if (game != null)
            {
                if (game.gameStarted)
                {
                    gameStartedChecker.Stop();
                    GameLoaded();
                    gameStoppedChecker.Start();
                }
            }
        }

        void gameStoppedChecker_Tick(object sender, EventArgs e)
        {
            if (!game.gameStarted)
            {
                gameStoppedChecker.Stop();
                game.Dispose();
                this.Close();
            }
        }

        void GameLoaded()
        {
            tvKomponenty_Load();
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            if(game != null) game.Exit();
        }
        #endregion

        #region Kamery
        private void btnFreeCamera_Click(object sender, RoutedEventArgs e)
        {
            game.freeCamera.Position = game.camera.Position;
            game.camera = game.freeCamera;
        }

        private void btnChaseCamera_Click(object sender, RoutedEventArgs e)
        {
            game.camera = game.chaseCamera;
        }

        private void btnTargetCamera_Click(object sender, RoutedEventArgs e)
        {
            game.targetCamera.Position = game.camera.Position;
            game.camera = game.targetCamera;
        }

        private void btnArcBallCamera_Click(object sender, RoutedEventArgs e)
        {
            game.camera = game.arcBallCamera;
        } 
        #endregion

        #region Bryły otaczające
        private void cbPokazSfery_Checked(object sender, RoutedEventArgs e)
        {
            foreach (Projekt.Models.CustomModel model in game.models)
            {
                model.Components[0].Visible = true;
            }
        }

        private void cbPokazBoxy_Checked(object sender, RoutedEventArgs e)
        {
            foreach (Projekt.Models.CustomModel model in game.models)
            {
                model.Components[1].Visible = true;
            }
        }

        private void cbPokazSfery_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (Projekt.Models.CustomModel model in game.models)
            {
                model.Components[0].Visible = false;
            }
        }

        private void cbPokazBoxy_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (Projekt.Models.CustomModel model in game.models)
            {
                model.Components[1].Visible = false;
            }
        } 
        #endregion

        #region Komponenty
        private void tvKomponenty_Load()
        {
            TreeViewItemType nodes = new TreeViewItemType();
            nodes.Header = "CustomModels";

            foreach (Projekt.Models.CustomModel cModel in game.models)
            {
                TreeViewItemType model = new TreeViewItemType();
                model.Header = cModel.ToString();
                model.type = cModel;

                foreach (Projekt.Models.Primitives.Primitive primitive in cModel.Components)
                {
                    TreeViewItemType compo = new TreeViewItemType();
                    compo.Header = primitive.ToString();
                    compo.type = primitive;
                    model.Items.Add(compo);
                }

                nodes.Items.Add(model);
            }

            tvKomponenty.Items.Add(nodes);
        }

        private void tvKomponenty_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            tbDaneKomponentuUpdater.Stop();

            if (((TreeViewItemType)tvKomponenty.SelectedItem).type != null)
            {
                gbDaneKomponentu.Visibility = System.Windows.Visibility.Visible;
                gbOpcjeKomponentu.Visibility = System.Windows.Visibility.Visible;

                tbDaneKomponentuUpdater.Start();
            }
        }

        void tbDaneKomponentuUpdater_Tick(object sender, EventArgs e)
        {
            string data = "";

            lblAMax.Content = Player.AMax;
            lblACurrent.Content = Player.ACurrent;

            if (((TreeViewItemType)tvKomponenty.SelectedItem).type != null)
            {
                if (((TreeViewItemType)tvKomponenty.SelectedItem).type.GetType() == typeof(Projekt.Models.CustomModel))
                {

                    data += "POZYCJA: \nX: " + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Position.X + "\nY: " + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Position.Y + "\nZ: " + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Position.Z + "\n";
                    data += "ROTACJA: \nX: " + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Rotation.X + "\nY: " + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Rotation.Y + "\nZ: " + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Rotation.Z + "\n";
                    data += "SKALA: \nX: " + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Scale.X + "\nY: " + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Scale.Y + "\nZ: " + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Scale.Z + "\n";

                    data += "VISIBLE: \n" + ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Visible.ToString() + "\n";

                    data += "KOMPONENTY: \n";
                    foreach (Projekt.Models.Primitives.Primitive primitive in ((Projekt.Models.CustomModel)((TreeViewItemType)tvKomponenty.SelectedItem).type).Components)
                    {
                        data += primitive.ToString() + "\n";
                    }
                }
                else if (((TreeViewItemType)tvKomponenty.SelectedItem).type.GetType() == typeof(Projekt.Models.Primitives.Sphere))
                {

                    data += "POZYCJA: \nX: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Position.X + "\nY: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Position.Y + "\nZ: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Position.Z + "\n";
                    data += "ROTACJA: \nX: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Rotation.X + "\nY: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Rotation.Y + "\nZ: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Rotation.Z + "\n";
                    data += "SKALA: \nX: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Scale.X + "\nY: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Scale.Y + "\nZ: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Scale.Z + "\n";

                    data += "VISIBLE: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Visible.ToString() + "\n";
                }
                else if (((TreeViewItemType)tvKomponenty.SelectedItem).type.GetType() == typeof(Projekt.Models.Primitives.Box))
                {

                    data += "POZYCJA: \nX: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Position.X + "\nY: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Position.Y + "\nZ: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Position.Z + "\n";
                    data += "ROTACJA: \nX: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Rotation.X + "\nY: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Rotation.Y + "\nZ: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Rotation.Z + "\n";
                    data += "SKALA: \nX: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Scale.X + "\nY: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Scale.Y + "\nZ: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Scale.Z + "\n";

                    data += "VISIBLE: " + ((Projekt.Models.Primitives.Primitive)((TreeViewItemType)tvKomponenty.SelectedItem).type).Visible.ToString() + "\n";
                }
            }
            else
            {
                tbDaneKomponentuUpdater.Stop();
                gbDaneKomponentu.Visibility = System.Windows.Visibility.Collapsed;
                gbOpcjeKomponentu.Visibility = System.Windows.Visibility.Collapsed;
            }

            tbDaneKomponentu.Text = data;
        }

        private void btnWlWylObiekt_Click(object sender, RoutedEventArgs e)
        {
            if (((DrawableGameComponent)((TreeViewItemType)tvKomponenty.SelectedItem).type).Visible)
                ((DrawableGameComponent)((TreeViewItemType)tvKomponenty.SelectedItem).type).Visible = false;
            else
                ((DrawableGameComponent)((TreeViewItemType)tvKomponenty.SelectedItem).type).Visible = true;
        } 
        #endregion
    }
}
